package com.fubowen.reader.extension

import android.content.Context
import android.view.Window
import android.view.WindowManager
import androidx.annotation.IntRange
import kotlin.math.roundToInt

fun Context.dpToPxFloat(dp: Int) = (dp * this.resources.displayMetrics.density)
fun Context.dpToPx(dp: Int) = dpToPxFloat(dp).roundToInt()
val Context.statusBarHeight: Int
    get() {
        var statusBarHeight = 0
        val resources = resources
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            statusBarHeight = resources.getDimensionPixelSize(resourceId)
        }
        return statusBarHeight
    }

fun Window.setScreenBrightness(@IntRange(-1, 255) value: Int) {
    val lp = this.attributes
    if (value < 0) {
        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
    } else {
        lp.screenBrightness = value / 255f
    }
    this.attributes = lp
}

